mport java.util.Random;


public class Calculations {
	
	private static final int cov=0; 
	private static final int corr=1; 
	
	public double[] createRandVect(int size){
		Random r = new Random();
		double[] arr = new double[size];
		for(int i=0; i<size; i++)
			arr[i]=r.nextDouble();
		return arr;
	}
	
	public double calcSimpleReturn(double initVal, double finalVal){
		double r;
		r = (finalVal-initVal)/initVal;
		return r;
	}
	
	public double logreturn(double initVal, double finalPrice){
		double r;
		r = Math.log(finalPrice/initVal);
		return r;
	}
	
	public int[] readNrShares(int nStocks){
		int[] arr = new int[nStocks];	
		for(int i=0; i<nStocks; i++){
			//read from DB 
			int n=0;
			arr[i]=n;
		}
		return arr;
	}
	
	public double[] readSharePrices(int nStocks){
		double[] arr = new double[nStocks];	
		for(int i=0; i<nStocks; i++){
			//read from DB using JDBC
			double d=0;
			arr[i]=d;
		}
		return arr;
	}
	
	//this method must be called for each stock belongs to trader X.
	public double stockVal(int nShares, double sharePrice){
		double v = nShares*sharePrice;
		return v;
	}
	
	public double[] createStockValArr(int[] nShares, double[] sharePrice){
		int nStocks = nShares.length;
		double[] arr =  new double[nStocks];
		for (int i=0; i<nStocks; i++){
			arr[i]=nShares[i]*sharePrice[i];
		}
		return arr;
	}
	
	
	//we assume that at 8:00 in the morning, everyone has got 100 Pounds to invest.
	//so the initial value of the portfolio is 100.
	public double portfolioVal(double[] stockValsArr){
		double v=0;
		for (int i=0; i<stockValsArr.length; i++){
			v+=stockValsArr[i];
		}
		return v;
	}
	
	public void storePortfolioVals(int nTraders){
		for (int i=0; i<nTraders; i++){
			//store in DB using JDBC
		}
	}
	
	public double calcPortReturn(){
		//calculate the prortfolio value of the current time using  "portfolioVal" method.
		double pV2=0;
		//get the prortfolio value from the DB using JDBC.
		double pV1=0;
		double rSimp=calcSimpleReturn(pV1, pV2);
		double rLog=logreturn(pV1, pV2);
		return rSimp;
	}
	
	//each position of nCommentsArr represents the number of stars for a certain comment.
	public double calcAvStars(int[] nCommentsArr){
		double av=0;
		for(int i=0; i<nCommentsArr.length; i++)
			av+=nCommentsArr[i];
		av/=nCommentsArr.length;
		//an extra step which might be neglected.
		av*=Math.log(nCommentsArr.length+1);//we added 1 to prevent log(0).
		return av;
	}
	
	public double calcVariance(double[] array)  {
		double mean = 0;
		double v = 0;
		for (int n=0; n<array.length; n++) {
		    double x = array[n];
		    double delta = x - mean;
		    mean += delta/(n+1);//mean(n+1)=mean(n)+[x(n+1)-mean(n)]/(n+1)
		    v += delta*(x - mean);
		    } 
		v/=array.length-1;
		return (v);
	}
	
	public double calcSD(double[] array){
		double sd = Math.sqrt(calcVariance(array));
		return sd;
	}
	
	public double[] calc_Cov_Corr(double[] series_x, double[] series_y){
		
		double[] results = {0,0};
		double var_x = 0;
		double var_y = 0;
		double sum_coproduct = 0;
		double mean_x = 0;
		double mean_y = 0;
		for(int n=0;n<series_x.length;n++){
			double delta_x = series_x[n]-mean_x;
			double delta_y = series_y[n]-mean_y;
			mean_x += delta_x/(n+1);
			mean_y += delta_y/(n+1);
			var_x += delta_x * (series_x[n]-mean_x);
			var_y += delta_y * (series_y[n]-mean_y);
			sum_coproduct += (delta_x*(series_y[n]-mean_y)+delta_y*(series_x[n]-mean_x))/2;
			
		}
		double sd_x = (double) Math.sqrt(var_x/(series_x.length-1));
		double sd_y = (double) Math.sqrt(var_y/(series_y.length-1));
		double cov_x_y = sum_coproduct/(series_x.length-1);
		results[cov]=cov_x_y;
		results[corr] = cov_x_y / (sd_x*sd_y);
		return results;
	}
	
	public void printOut(String name, double[] v){
		System.out.println(name);
		for(int i=0; i<v.length; i++)
			System.out.println(v[i]);
	}
	
	//we calculate the risk of the portfolio based on the market value of the portfolio, not the initial investment amount.
	public double calcPortRisk(double[] stockValsArr){
		double portRisk=0;
		for(int i=0; i<stockValsArr.length; i++){
			for(int j=0; j<stockValsArr.length; j++){
				double[] series_x = null;// = getHistPrices();
				double[] series_y = null;// = getHistPrices();
				portRisk+=stockValsArr[i]*stockValsArr[j]*calc_Cov_Corr(series_x, series_y)[cov];	
			}//j
		}//i
	return portRisk;		
		
	}
	
	public double calcSharpeRatio(double portRet, double portRisk){
		double s = portRet/portRisk;
		return s;
	}

}






























